package com.hpeng.sys.service.impl;

import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Component;

import com.hpeng.daily.dao.AssetCardBookDao;
import com.hpeng.sys.dao.AssetCardBooksDao;
import com.hpeng.sys.dao.AssetCardsDao;
import com.hpeng.sys.dao.ProdStatusDao;
import com.hpeng.sys.dao.SysTemplateDao;
import com.hpeng.sys.dao.SysUserDao;
import com.hpeng.sys.model.AssetCard;
import com.hpeng.sys.model.AssetCardBook;
import com.hpeng.sys.model.Department;
import com.hpeng.sys.model.ProdStatusMst;
import com.hpeng.sys.model.Staff;
import com.hpeng.sys.model.SysTemplate;
import com.hpeng.sys.model.SysUser;
import com.hpeng.sys.service.AssetCardsMgr;
import com.hpeng.sys.service.CategoryMgr;
import com.hpeng.sys.service.DepartmentsMgr;
import com.hpeng.sys.service.LoginUserMgr;
import com.hpeng.sys.service.StaMgr;

@Component("assetCardsMgr")
public class AssetCardsMgrImpl implements AssetCardsMgr{
	public List<AssetCard> getInuseAssetCardByCatId(String catId){
		return assetCardsDao.getInuseAssetCardByCatId(catId);
	}
	public List<AssetCard> getAssetCardByDepName(String departmentId){
		System.out.println(departmentId+"ssss");
		if(departmentId.equals("000")){
			return assetCardsDao.getByDepName("");
		}else{
			Department department = departmentsMgr.getById(departmentId);
			System.out.println(department);
			return assetCardsDao.getByDepName(department.getDepsimplename());
		}
	}
	public List<AssetCard> getByAccountSubject(String accountSubject){
		System.out.println(accountSubject);
		return assetCardsDao.getByAccountSubject(accountSubject);
	}
	public List<AssetCard> getByDepAndStatus(String userStaId,String TemplateCode){
		Staff staff = staMgr.getById(userStaId);
		SysTemplate systemplate = sysTemplateDao.getById(TemplateCode);
		System.out.println(systemplate.getTemplateName().equals("超级管理员"));
		String flag = null;
		if(systemplate.getTemplateName().equals("普通部门领导")){
			flag = "BMLD";
		}else if(systemplate.getTemplateName().equals("普通部门经办员")){
			flag = "JB";
		}else if(systemplate.getTemplateName().equals("分管局领导")){
			flag = "JLD";
		}else if(systemplate.getTemplateName().equals("超级管理员")){
			flag = "ALL";
		}
		String[] aaa = prodStatusDao.getByStatus(flag);
		String deplName = staff.getDepartment().getDepname();
		List<AssetCard> assetCards = assetCardsDao.getByDepAndStatus(deplName,aaa);
		return assetCards;
	}
	public boolean ToGo(List<String> assetCardIds){
		AssetCard assetCard = null;
		AssetCardBook assetCardBook =null;
		ProdStatusMst prodStatus = null;
		String over = null;
		String main = null;
		String statusCode = null;
		boolean flag = false;
		boolean result = false;
		for(int i = 0;i < assetCardIds.size();i++){
		assetCard = assetCardsDao.getByAssetCardId(assetCardIds.get(i));
		statusCode = assetCard.getAcstatus();
		main = statusCode.substring(0,1);
		over = statusCode.substring(1,3);
		System.out.println(main+"   " + over);
		prodStatus = prodStatusDao.getStatus(main,over);
		flag = assetCardsDao.UpdateStatus(assetCardIds.get(i), prodStatus);
			if(flag==true){
				assetCardBook = new AssetCardBook();
				assetCardBook.setCard(assetCard);
				assetCardBook.setOperator(loginUserMgr.getUserName(ServletActionContext.getRequest()));
				assetCardBook.setOperatedate(new Date());
				assetCardBook.setAcprimarystatus(prodStatus.getStatuscode());
				assetCardBook.setAcsecondstatus(prodStatus.getSubcode());
				result = assetCardBooksDao.add(assetCardBook);
			}
		}
		return result;
	}
	@Resource
	private AssetCardBooksDao assetCardBooksDao;
	@Resource
	private LoginUserMgr loginUserMgr;
	@Resource
	private SysTemplateDao sysTemplateDao;
	@Resource
	private StaMgr staMgr;
	@Resource
	private ProdStatusDao prodStatusDao;
	@Resource
	private DepartmentsMgr departmentsMgr;
	@Resource
	private AssetCardsDao assetCardsDao;
}